package com.yuelao.yuelao_backend.controller;

import com.yuelao.yuelao_backend.common.ApiResponse;
import com.yuelao.yuelao_backend.entity.VipPlan;
import com.yuelao.yuelao_backend.service.VipService;
import com.yuelao.yuelao_backend.util.UserContextUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * VIP会员Controller
 */
@RestController
@RequestMapping("/vip")
@Slf4j
@RequiredArgsConstructor
public class VipController {

    private final VipService vipService;

    /**
     * 获取VIP特权列表
     * GET /vip/benefits
     * @return 特权列表
     */
    @GetMapping("/benefits")
    public ApiResponse<List<Map<String, Object>>> getBenefits() {
        List<Map<String, Object>> benefits = vipService.getBenefits();
        return ApiResponse.ok(benefits);
    }

    /**
     * 获取VIP套餐列表
     * GET /vip/plans
     * @return 套餐列表
     */
    @GetMapping("/plans")
    public ApiResponse<List<VipPlan>> getPlans() {
        List<VipPlan> plans = vipService.getPlans();
        return ApiResponse.ok(plans);
    }

    /**
     * 购买VIP
     * POST /vip/purchase
     * @param request 购买请求
     * @return 订单信息
     */
    @PostMapping("/purchase")
    public ApiResponse<Map<String, Object>> purchaseVip(@Valid @RequestBody PurchaseVipRequest request) {
        Long userId = UserContextUtil.getCurrentUserId();
        Map<String, Object> result = vipService.purchaseVip(
            userId, 
            request.getPlanId(), 
            request.getPaymentMethod()
        );
        return ApiResponse.ok(result);
    }

    /**
     * 购买VIP请求DTO
     */
    @lombok.Data
    public static class PurchaseVipRequest {
        @NotNull(message = "套餐ID不能为空")
        private Long planId;
        
        @jakarta.validation.constraints.NotBlank(message = "支付方式不能为空")
        private String paymentMethod; // wechat/alipay/coin
    }
}

